<template>
  <header id="header" class="d-none d-md-block">
    <nav class="new-nav-first container-fluid">
      <div class="row container m-auto">
        <div class="brand col-30 col-md-20" click="redirect" data-href="/">91 <span>PORNY</span><span
            class="flag d-none d-md-inline-block">九色</span></div>
        <form action="/search" method="GET"
              class="search-form form col-md-18 d-none d-md-flex px-0 justify-content-end align-items-center">
          <div class="input-group">
            <input name="keywords" class="form-control" type="search" value>
            <div class="input-group-append cursor-p" needAuth>
              <span class="input-group-text"><i class="border-0 fas fa-search"></i></span>
            </div>
          </div>
        </form>
        <div class="options col-30 col-md-22">
          <div class="item login-btn cursor-p">
            <div class="icon">
              <i class="fa fa-icon-user"></i>
            </div>
            <div class="text">登录</div>
          </div>
          <div class="item register-btn cursor-p">
            <div class="icon">
              <i class="el-icon-user"></i>
            </div>
            <div class="text">注册</div>
          </div>
        </div>
      </div>
    </nav>
    <nav id="navi" class="nav-second container">
      <ul class="nav">
        <li class="nav-item d-none d-md-block">
          <a class="nav-link d-flex align-items-center " href="/">
            首页 </a>
        </li>
        <li class="nav-item ">
          <a class="nav-link d-flex align-items-center " href="/video">
            视频 </a>
        </li>
        <li class="nav-item ">
          <a class="nav-link d-flex align-items-center " href="/videos">
            蝌蚪 </a>
        </li>
        <li class="nav-item ">
          <a class="nav-link d-flex align-items-center active" href="/blog">
            论坛 </a>
        </li>
        <li class="nav-item ">
          <a class="nav-link d-flex align-items-center " href="/premium">
            高清VIP </a>
        </li>
        <li class="nav-item ">
          <a class="nav-link d-flex align-items-center " href="/app">
            App下载 </a>
        </li>
      </ul>
      <div class="leftBtn d-md-none">
        <i class="fa fa-chevron-right"></i>
      </div>
    </nav>
  </header>
  <header class="Mobile-Header d-block d-md-none">
    <nav class="Nav d-flex align-items-center justify-content-between">
      <div class="Menu-Btn"><i class="fa fa-align-justify"></i></div>
      <div class="Brand" click="redirect" data-href="/">91 <span>PORNY</span><span class="Flag">九色</span></div>
      <div class="Search-Btn cursor-p"><i class="fa fa-search"></i></div>
    </nav>
    <form action="/search" method="GET" class="SearchForm form justify-content-end align-items-center">
      <div class="input-group">
        <input name="keywords" class="form-control" type="search" value>
        <div class="input-group-append cursor-p" needAuth>
          <span class="input-group-text"><i class="border-0 fas fa-search"></i></span>
        </div>
      </div>
    </form>
    <div class="Menu">
      <div class="BrandMenu">
        <div class="row">
          <div class="Item login-btn">
            <i class="fa  fa-sign-in-alt"></i>
            <div class="text">登录</div>
          </div>
          <div class="Item register-btn">
            <i class="el-icon-user-solid"></i>
            <div class="text">注册</div>
          </div>
        </div>
      </div>
      <div class="Title"><span>菜单</span><i class="fa fa-angle-up"></i></div>
      <div id="mnavi" class="Items">
        <a class="Item " href="/video">
          <i class="fa fa-video icon"></i><span>视频</span>
        </a>
        <a class="Item " href="/videos">
          <i class="fa fa-play icon"></i><span>蝌蚪</span>
        </a>
        <a class="Item active" href="/blog">
          <i class="fa fa-book icon"></i><span>论坛</span>
        </a>
        <a class="Item " href="/premium">
          <i class="fa fa-medal icon"></i><span>高清VIP</span>
        </a>
        <a class="Item " href="/app">
          <i class="fa fa-mobile-alt icon"></i><span>App下载</span>
        </a>
        <a target="_blank" class="Item " rel="nofollow noopener" href="https://theporndude.com/zh">
          <i class="fa fa-star-of-david icon"></i><span>ThePornDude</span>
        </a>
      </div>
    </div>
  </header>


  <main id="main">
    <div id="videoListPage" class="container-fluid p-0" xmlns="http://www.w3.org/1999/html">
      <div class="row">
        <div class="cateContainer col-lg-11 d-none d-lg-block">
          <ul>
            <li class="title"><i class="fa fa-list"></i> 论坛版块</li>
            <li  v-for="(blogCategory, index) in blogCategorys" :key="index"
                 @click="changeBlogCategory(blogCategory.blogType)" :class="blogCategory.blogType == blogType ? 'active' : 'orange'" >
                <i class="fa fa-book "></i> {{ blogCategory.name }}
            </li>
            <li click="redirect" data-href="https://bbs.9se.club"><i class="fa fa-bullhorn"></i> 我要发帖</li>
          </ul>
        </div>
        <div class="col-lg-1"></div>
        <div class="col-60 col-lg-48">
          <div class="title row navTab ">
            <div class="navContainer sortType col-60 col-md-40 mb-3">
              <a :class="'' == filter ? 'active' : 'orange'" @click="searchBlogK('')">全部</a>
              <a :class="'digest' == filter ? 'active' : 'orange'" @click="searchBlogK('digest')">精华</a>
              <a :class="'day' == filter ? 'active' : 'orange'"  @click="searchBlogK('day')">一天</a>
              <a :class="'week' == filter ? 'active' : 'orange'" @click="searchBlogK('week')">周</a>
              <a :class="'month' == filter ? 'active' : 'orange'"  @click="searchBlogK('month')">月</a>
              <a :class="'month' == filter ? 'hot' : 'orange'" @click="searchBlogK('hot')">热门</a>
            </div>
            <div class="total col-md-20 d-none d-md-block ">
              <small>版块主题：第{{ pageNum }}页</small>
            </div>
          </div>
          <div class="row">

            <div class="colNovelList" v-for="(blog, index) in blogs" :key="index">
              <a target="_self" class="novelElem" :href="blog.sourceUrl">
                <div class="container-title" :text="blog.title"> {{ blog.title }}</div>
                <div class="info text-small">
                        <span class="mr-2">
                        <i class="fa  fa-eye mr-1"></i>
                        <span>{{ blog.totalViews }}</span>
                        </span>
                                          <span class="mr-2">
                        <i class="fa fa-thumbs-up mr-1"></i>
                        <span>{{ blog.totalLinks }}</span>
                        </span>
                                          <span class="mr-2">
                        <i class="fa fa-user mr-1"></i>
                        <span th:text="${blog.username}">{{ blog.username }}</span>
                        </span>
                                          <span>
                        <i class="fa fa-clock mr-1"></i>
                        <span>{{ blog.createDate }}</span>
                        </span>
                </div>

                <div class="type" th:text="${blog.categoryName}">
                  {{ blog.categoryName }}
                </div>
              </a>
            </div>
          </div>

          <div class="block">
            <el-pagination
                @current-change="handleCurrentChange"
                @prev-click="handlePrevClick"
                @next-click="handleNextClick"
                :current-page.sync="pageNum"
                :page-size="pageSize"
                layout="prev, pager, next, jumper"
                :total="total">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </main>

  <!--  <Footer />-->
</template>

<script>
import "@/assets/styles/book.css";
import {reactive, toRefs, onMounted, ref} from "vue";
import {useRouter, useRoute} from "vue-router";
import {searchBooks, listCategorys, searchBlogs,listBlogCategorys} from "@/api/book";
import {addDay, dateFormat} from "@/utils";
import Top from "@/components/common/Top";
import Navbar from "@/components/common/Navbar";
import Footer from "@/components/common/Footer";

export default {
  name: "bookclass",
  components: {
    Top,
    Navbar,
    Footer,
  },
  setup() {
    const route = useRoute();
    const router = useRouter();

    const state = reactive({
      blogType: 1,
      filter: '',
      blogs: [],
      bookCategorys: [],
      blogCategorys: [],
      books: [],
      searchCondition: {},
      backgroud: true,
      pageNum : 1,
      total: 0,
      pageSize: 10,
      imgBaseUrl: process.env.VUE_APP_BASE_IMG_URL,
      workDirectionOn: 0,
      categoryOn: 0,
      bookStatusOn: null,
      wordCountOn: null,
      updateTimeOn: null,
      sortOn: null
    });
    onMounted(() => {
      const key = route.query.key;
      state.blogType = 4;
      state.searchCondition.keyword = key;
      state.searchCondition.blogType = state.blogType;
      state.searchCondition.pageSize = 10;
      loadblogCategorys();
      searchBlog();
      // loadCategoryList(0);
    });

    const search = async () => {
      const {data} = await searchBooks(state.searchCondition);
      state.books = data.list;
      state.searchCondition.pageNum = data.pageNum;
      state.searchCondition.pageSize = data.pageSize;
      state.total = Number(data.total);
    };

    const searchBlog = async () => {
      const {data} = await searchBlogs(state.searchCondition);
      state.blogs = data.list;
      state.searchCondition.pageNum = data.pageNum;
      state.searchCondition.pageSize = data.pageSize;
      state.total = Number(data.total);
    };

    const searchByK = (key) => {
      state.searchCondition = {keyword: key};
      search();
    };
    const bookDetail = (bookId) => {
      router.push({path: `/book/${bookId}`});
    };

    const handleCurrentChange = (pageNum) => {
      state.searchCondition.pageNum = pageNum;
      state.pageNum = pageNum;
      searchBlog();
    };

    const handlePrevClick = (pageNum) => {
      state.searchCondition.pageNum = pageNum-1;
      state.pageNum = pageNum-1;
      searchBlog();
    };

    const handleNextClick = (pageNum) => {
      let newpage = pageNum +1;
      state.searchCondition.pageNum = newpage;
      state.pageNum = newpage;
      searchBlog();
    };

    const loadCategoryList = async (workDirection) => {
      const {data} = await listCategorys({workDirection: workDirection});
      state.bookCategorys = data;
      state.workDirectionOn = workDirection;
      state.searchCondition.workDirection = workDirection;
      state.categoryOn = 0;
      state.searchCondition.categoryId = null;
      search();
    };

    const loadblogCategorys = async () => {
      const {data} = await listBlogCategorys();
      state.blogCategorys = data;
    };

    const changeCategory = async (categoryId) => {
      state.categoryOn = categoryId;
      if (categoryId > 0) {
        state.searchCondition.categoryId = categoryId;
      } else {
        state.searchCondition.categoryId = null;
      }
      search();
    };

    const changeBookStatus = async (bookStatus) => {
      state.bookStatusOn = bookStatus;
      state.searchCondition.bookStatus = bookStatus;
      search();
    };

    const changeWordCount = async (wordCountMin, wordCountMax) => {
      state.wordCountOn = wordCountMin;
      state.searchCondition.wordCountMin = wordCountMin;
      state.searchCondition.wordCountMax = wordCountMax;

      search();
    };

    const changeUpdateTime = async (days) => {
      state.updateTimeOn = days;
      if (days) {
        state.searchCondition.updateTimeMin = dateFormat(
            "YYYY-mm-dd",
            addDay(-days)
        );
      } else {
        state.searchCondition.updateTimeMin = null;
      }
      search();
    };

    const changeSort = async (sort) => {
      state.sortOn = sort;
      state.searchCondition.sort = sort;
      search();
    };

    const changeBlogCategory = async (blogCategoryType) => {
      state.blogType = blogCategoryType;
      state.searchCondition.blogType = blogCategoryType;
      searchBlog();
    };

    const searchBlogK = async (filter) => {
      state.filter = filter;
      state.searchCondition.filter = filter;
      searchBlog();
    };

    return {
      ...toRefs(state),
      bookDetail,
      searchByK,
      search,
      handleCurrentChange,
      loadCategoryList,
      changeCategory,
      changeBookStatus,
      changeWordCount,
      changeUpdateTime,
      changeSort,
      searchBlog,
      handlePrevClick,
      handleNextClick,
      changeBlogCategory,
      searchBlogK
    };
  },
  computed: {
    wordCountFormat(wordCount) {
      return (wordCount) => {
        if (wordCount.length >= 5) {
          return parseInt(wordCount / 10000) + "万";
        }
        if (wordCount.length >= 4) {
          return parseInt(wordCount / 1000) + "千";
        }
        return wordCount;
      };
    },
  },
};
</script>

<style>
.el-pagination {
  justify-content: center;
}

.el-pagination.is-background .el-pager li:not(.is-disabled).is-active {
  background-color: #f80 !important;
}

.el-pagination {
  --el-pagination-hover-color: #f80 !important;
}
</style>

<style>
.pagination {
  flex-wrap: wrap;
}
</style>
